package com.pointtown.vcrss.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.pointtown.vcrss.R;
import com.pointtown.vcrss.database.Entry;
import com.pointtown.vcrss.database.RSSTabeleDAO;

public class ListActivity extends android.app.ListActivity {
	private static final String TAG = "ListActivity";
	private TableAdapter adapter;
	private String pattern[] = { DateUtils.PATTERN_RFC1123 };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		adapter = new TableAdapter(this);
		setListAdapter(adapter);
		
		RSSTabeleDAO dao = new RSSTabeleDAO(this);
		ArrayList<Entry> list = dao.getAllEntries();
		
		//Log.v(TAG, list.size() + "");
		
		for(Entry entry:list){
			adapter.add(entry);
		}
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("id", id);
		startActivity(intent);
	}
	
	class TableAdapter extends ArrayAdapter<Entry>{
		
		public TableAdapter(Context context) {
			super(context, R.layout.list_row);
		}
		
		@Override
		public long getItemId(int position) {
			Entry item = getItem(position);
			return Integer.parseInt(item.getId());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			Entry item = getItem(position);
			Holder holder = null;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.list_row_rich, null);
				
				//追加
				TextView title = (TextView)row.findViewById(R.id.text_title);  
		        title.setTypeface(Typeface.DEFAULT_BOLD);
		        TextView date = (TextView)row.findViewById(R.id.text_date);
		        
		        holder = new Holder();
		        holder.title = title;
		        holder.date = date;
		        
		        row.setTag(holder);
		        
			}
			
			if (item != null) {
				holder = (Holder)row.getTag();
			}
			//Entry item = (Entry)items.get(position);  
		    holder.title.setText(item.getTitle());
		    
		    String pubdate = item.getPubDate().toString();
		    
		    String formattedPubdate = "";  
		    try {  
		     Date date = DateUtils.parseDate(pubdate, pattern);  
		     SimpleDateFormat sdf = new SimpleDateFormat("M'月' d'日', yyyy 'at' HH:mm");  
		     formattedPubdate = sdf.format(date);  
		    } catch (DateParseException e) {  
		    	formattedPubdate = pubdate; 
		    }
		    holder.date.setText(formattedPubdate);
		    Log.v(TAG, holder.toString());
		    
			
			return row;
		}
	}
	
	static class Holder{
		TextView title;
		TextView date;
		
		@Override
		public String toString() {
			return "Holder [title=" + title + ", date=" + date + "]";
		}
	}
}
